﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.Authorization;
using jSLCore.Client.Presentation.BaseControl.Selection;

namespace jSLApp.Client.Forms
{
    public partial class MessageList : SelectControl
    {
        private readonly string _login;

        /// <summary>
        /// for designers only
        /// </summary>
        public MessageList()
        {
            InitializeComponent();
        }

        public MessageList(string login)
        {
            _login = login;
            InitializeComponent();
        }

        public override void LoadInitialData()
        {
            Search();
        }

        private void Search()
        {
            DoWork("Загрузка входящих сообщений",(sender, args) =>
                                                     {
                                                         args.Result = Communication.Proxy.GetUserMessages(args.String,
                                                                                                           (string) args.Params[1], true);
                                                     },(o, eventArgs) =>
                                                                           {
                                                                               if(eventArgs.Error==null)
                                                                               {
                                                                                   inGrid.ItemsSource = (IEnumerable) eventArgs.Result;
                                                                               }
                                                                           },_login,txt.Text);

            DoWork("Загрузка исходящих сообщений", (sender, args) =>
            {
                args.Result = Communication.Proxy.GetUserMessages(args.String,
                                                                  (string)args.Params[1], false);
            }, (o, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    outGrid.ItemsSource = (IEnumerable)eventArgs.Result;
                }
            }, _login, txt.Text);
        }

        #region dialog

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }

        protected override void AcceptButtonClicked()
        {
            Search();
        }

        public override ISelectableGrid GetSelectableGrid()
        {
            return inGrid;
        }

        public override bool AutoSize
        {
            get { return false; }
        }

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override bool IsOkVisibleWhenNotSelectable
        {
            get { return false; }
        }

        public override object CancelContentWhenNotSelectable
        {
            get { return "Закрыть"; }
        }

        #endregion

        private void reply_Clicked(object sender, RowSelectedEventArgs e)
        {
            var message = (Message)e.RowObject;
            var dlg = new NewMessage(message.id);
            dlg.ShowDialog(delegate(object o, object data)
            {
                LoadInitialData();
            },null);
        }

        private void createMessage_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new NewMessage();
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   LoadInitialData();
                               },null );
        }
    }
}
